package com.bw.zhanggm.file.controller;


import cn.hutool.json.JSONUtil;
import com.bw.itext.pdf.service.PdfService;
import com.bw.zhanggm.file.config.AjaxResult;
import com.bw.zhanggm.file.entity.HtContract;
import com.bw.zhanggm.file.service.IHtContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 合同信息表 前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2024-01-12
 */
@Slf4j
@RestController
@RequestMapping("/hetong")
public class HtContractController {

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private IHtContractService iHtContractService;

    @Autowired
    private PdfService pdfService;


    @GetMapping("list")
    public AjaxResult list(HttpServletRequest request) throws IOException {
        List<HtContract> list = iHtContractService.list();
        return AjaxResult.success(list);
    }

    @GetMapping("download")
    public void download(HttpServletRequest request) throws IOException {
        List<HtContract> list = iHtContractService.list();
        pdfService.export(list,"hetong.pdf","合同列表",HtContract.class);
        pdfService.download("hetong.pdf", response);
    }

    @GetMapping("export")
    public void export() throws IOException {
        List<HtContract> list = iHtContractService.list();
        pdfService.export(list,"hetong.pdf","合同列表",HtContract.class);
    }

    @GetMapping("preview")
    public void preview() throws IOException {
        List<HtContract> list = iHtContractService.list();
        log.info("list:{}", list);
        //生产pdf问题件
        pdfService.export(list,"hetong.pdf","合同列表",HtContract.class);
        //在pdf文件上，加电子签章
        pdfService.sign("hetong.pdf","hetong-sign.pdf");
        //浏览器里面打开pdf
        pdfService.preview("hetong-sign.pdf", response);
        //浏览器下载文件到本地
        pdfService.download("hetong.pdf", response);
    }



}
